<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="行号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lineNo">
              <a-input v-model="model.lineNo" placeholder="系统自动生成"  disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="物料" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="itemId">
              <j-popup
                v-model="model.itemName"
                field="itemId"
                org-fields="id,name"
                dest-fields="itemId,itemName"
                code="wms_item_report"
                :multi="false"
                @input="popupCallback"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="需求数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="demonQty">
              <a-input-number v-model="model.demonQty" placeholder="请输入需求数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="生产日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lot01">
              <j-date placeholder="请选择生产日期"  v-model="model.lot01" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="销售单" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lot02">
              <a-input v-model="model.lot02" placeholder="请输入销售单"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="供应商代码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lot03">
              <a-input v-model="model.lot03" placeholder="请输入供应商代码"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="ERP代码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lot04">
              <a-input v-model="model.lot04" placeholder="请输入ERP代码"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="等级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lot05">
              <a-input v-model="model.lot05" placeholder="请输入等级"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="档次" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lot06">
              <a-input v-model="model.lot06" placeholder="请输入档次"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="SN号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lot07">
              <a-input v-model="model.lot07" placeholder="请输入SN号"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import {httpAction, putAction} from '@/api/manage'
const ruleBaseURL = '/sys/fillRule/executeRuleByCode/'

  export default {
    name: 'WmsPickDetailForm',
    components: {
    },
    props: {
      selectObj:{
        type: Object,
        default: {},
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{lineNo:''},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          itemId:[
            { required: true, message: '请选择物料!' },
          ],
          demonQty:[
            { required: true, message: '请输入!' },
          ],
        },
        url: {
          add: "/wms/wmsPickDetail/add",
          edit: "/wms/wmsPickDetail/edit",
          queryById: "/wms/wmsPickDetail/queryById",
          rule: {
            lineNo: ruleBaseURL + 'wms_line_no_rule'
          },
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      popupCallback(value,row){
        this.model = Object.assign(this.model, row);
      },
      add () {
        this.genLineNo();
        this.edit(this.modelDefault);
      },
      genLineNo(){
        this.model.pickId = this.selectObj.id;
        putAction(this.url.rule.lineNo, this.model).then(res => {
          // 执行成功，获取返回的值，并赋到页面上
          if (res.success) {
            this.model.lineNo = res.result
          }
        })
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            that.model.pickId = that.selectObj.id;
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
        })
      },
    }
  }
</script>